import pygame
from settings import *
from random import randint, choice
from timer import Timer
"""
显示在屏幕的游戏对象的父类
"""
class Generic(pygame.sprite.Sprite):
    def __init__(self, pos, surf, groups, z=LAYERS['main']):
        super().__init__(groups)
        self.image = surf
        self.rect = self.image.get_rect(topleft=pos)
        self.z = z
        self.hitbox = self.rect.copy().inflate(-self.rect.width * 0.2, -self.rect.height * 0.75)

"""
角色交互
"""
class Interaction(Generic):
    def __init__(self, pos, size, groups, name):
        surf = pygame.Surface(size)
        super().__init__(pos, surf, groups)
        self.name = name
"""
水
"""
class Water(Generic):
    def __init__(self, pos, frames, groups):

        # 创立动画
        self.frames = frames
        self.frames_index = 0

        super().__init__(pos=pos,
                       surf=self.frames[self.frames_index],
                       groups=groups,
                       z=LAYERS['water'])

    def animate(self, dt):
        self.frames_index += 5 * dt
        if self.frames_index >= len(self.frames):
            self.frames_index = 0
        self.image = self.frames[int(self.frames_index)]

    def update(self, dt):
        self.animate(dt)

"""
野花
"""
class WildFlower(Generic):
    def __init__(self, pos, surf, groups):
        super().__init__(pos, surf, groups)
        self.hitbox = self.rect.copy().inflate(-20, -self.rect.height * 0.9)


"""
作物破坏时的显示画面
"""
class Particle(Generic):
    def __init__(self, pos, surf, groups, z, duration=200):
        super().__init__(pos, surf, groups, z)
        self.start_time = pygame.time.get_ticks()
        self.duration = duration

        # white surface
        mask_surf = pygame.mask.from_surface(self.image)
        new_surf = mask_surf.to_surface()
        new_surf.set_colorkey((0,0,0))
        self.image = new_surf

    def update(self, dt):
        current_time = pygame.time.get_ticks()
        if current_time - self.start_time > self.duration:
            self.kill()

"""
树
"""
class Tree(Generic):
    def __init__(self, pos, surf, groups, name, player_add):
        super().__init__(pos, surf, groups)

        # 苹果属性
        self.health = 5
        self.alive = True
        stump_path = f'../graphics/stumps/{"small" if name == "Small" else "large"}.png'
        self.stump_surf = pygame.image.load(stump_path).convert_alpha()
        self.invul_timer = Timer(200)

        # 苹果
        self.apples_surf = pygame.image.load('../graphics/fruit/apple.png')
        self.apple_pos = APPLE_POS[name]
        self.apple_sprites = pygame.sprite.Group()
        self.create_fruit()

        self.player_add = player_add

        # sounds
        self.axe_sound = pygame.mixer.Sound('../audio/axe.mp3')

    # 销毁树和摘苹果
    def damage(self):
        self.health -= 1

        # play sound
        self.axe_sound.play()
        # remove an apple
        if len(self.apple_sprites.sprites()) > 0:
            random_apple = choice(self.apple_sprites.sprites())
            Particle(pos=random_apple.rect.topleft,
                     surf=random_apple.image,
                     groups=self.groups()[0],
                     z=LAYERS['fruit'])
            self.player_add('apple')
            random_apple.kill()

    # 检查树是否死亡
    def check_death(self):
        if self.health <= 0:
            Particle(pos=self.rect.topleft,
                     surf=self.image,
                     groups=self.groups()[0],
                     z=LAYERS['fruit'],
                     duration=250)
            print(f">>>>>砍到一棵树")
            self.image = self.stump_surf
            self.rect = self.image.get_rect(midbottom=self.rect.midbottom)
            self.hitbox = self.rect.copy().inflate(-10,-self.rect.height * 0.6)
            self.alive = False
            self.player_add('wood')
    # 更新树的状态
    def update(self,dt):
        if self.alive:
            self.check_death()
    # 新的一天产生新的苹果
    def create_fruit(self):
        i = 0
        for pos in self.apple_pos:
            i += 1
            print(f'>>>>>创建苹果: {i} 次')
            if randint(0, 10) < 2:    #
                print("》》》》》》》进入创建苹果")
                x = pos[0] + self.rect.left
                y = pos[1] + self.rect.top
                Generic(pos=(x, y),
                        surf=self.apples_surf,
                        groups=[self.apple_sprites,self.groups()[0]],
                        z=LAYERS['fruit'])
